package com.stedu.mall.goodsservice.controller;

import cn.hutool.core.util.IdUtil;
import com.stedu.mall.common.bean.RespBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping({"/category","/goods"})
public class UploadController {

    @Value("${stedu.picdir}")
    private String picdir;

@PostMapping("/upload")
    public RespBean upload(MultipartFile pic) throws IOException {

        String filename = pic.getOriginalFilename();

        String suffix =filename.substring(filename.lastIndexOf('.'));

        filename= IdUtil.getSnowflakeNextIdStr()+suffix;

        pic.transferTo(new File(picdir+filename));

        return RespBean.ok("上传成功",filename);

    }
}
